<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018/8/22 0022
 * Time: 9:02
 */
namespace app\admin\controller;
use app\admin\model\Page;
use app\admin\model\UserModel;
use app\admin\model\VoucherModel;
use think\Request;

class Index
{
    /**加载后台首页
     * @param Request $request
     * @return string|\think\response\View
     */
    public function index(Request $request){
        $user = session('adminuser');
        if(!$user){
            return redirect('/admin/index/login');
        }
        return view('index',['adminuser'=>$user]);
    }

    /**加载登录页面
     * @param Request $request
     * @return \think\response\View
     */
    public function login(Request $request){
        return view('login');
    }

    /**后台账号登录接口
     * @param Request $request
     * @return \think\response\Json
     */
    public function sign(Request $request){
        $data = $request->param();
        $user = UserModel::getAdminUser()->where('adminuser',$data['adminuser'])->find();
        if(!$user){
            return json(['code'=>401,'msg'=>'账号没有注册']);
        }
        if($user['adminpwd']==getPwd($data['adminpwd'])){
            session('adminuser',$user);
            return redirect('/admin/index/index.html');
        }else{
            return json(['code'=>402,'msg'=>'账号或密码错误']);
        }
    }

    /**查询某部门的成员
     * @param Request $request:获取部门id
     * @return string|\think\response\Json
     */
    public function personnelList(Request $request){
        $department_id = $request->param('department_id',null);
        $company_id    = $request->param('company_id',null);
        if($department_id){
            $data = Page::IndexUser(['user.department_id'=>$department_id]);
        }elseif($company_id){
            $data = Page::IndexUser(['user.company_id'=>$company_id]);
        }else{
            $data = Page::IndexUser();
        }
        return json($data);
    }

    /**查询请假记录
     * @param Request $request
     * @return \think\response\Json
     */
    public function leaveList(Request $request){
        $department_id = $request->param('department_id',null);
        $company_id    = $request->param('company_id',null);
        if($department_id){
            $data = Page::Leave(['leave.departmen_id'=>$department_id]);
        }elseif($company_id){
            $data = Page::Leave(['leave.company_id'=>$company_id]);
        }else{
            $data = Page::Leave();
        }
        return json($data);
    }

    /**查询分公司列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function companyList(Request $request){
        $id = $request->param('id',null);
        if($id){
            $data = Page::Company(['id'=>$id]);
        }else{
            $data = Page::Company();
        }
        return json($data);
    }

    /**加载前台权限
     * @param Request $request
     * @return \think\response\Json
     */
    public function jurisdictionList(Request $request){
        $data = Page::Jurisdiction();
        return json($data);
    }

    /**查看公告
     * @param Request $request
     * @return \think\response\Json
     */
    public function noticeList(Request $request){
        $notice_type = $request->param('notice_type',0);
        if($notice_type==0){
            $data = Page::Notice();
        }else{
            $data = Page::Notice(['notice_type'=>$notice_type]);
        }
        return json($data);
    }

    /**查看报告
     * @param Request $request
     * @return \think\response\Json
     */
    public function reportList(Request $request){
        $department_id = $request->param('department_id',null);
        $company_id    = $request->param('company_id',null);
        $work_report_type = $request->param('work_report_type',null);
        if($department_id){
            if($work_report_type){
                $data = Page::Report(['work_report.department_id'=>$department_id,'work_report.work_report_type'=>$work_report_type]);
            }else{
                $data = Page::Report(['work_report.department_id'=>$department_id]);
            }
        }elseif ($company_id){
            if($work_report_type){
                $data = Page::Report(['work_report.company_id'=>$company_id,'work_report.work_report_type'=>$work_report_type]);
            }else{
                $data = Page::Report(['work_report.company_id'=>$company_id]);
            }
        }else{
            if($work_report_type){
                $data = Page::Report(['work_report.work_report_type'=>$work_report_type]);
            }else{
                $data = Page::Report();
            }
        }
        return json($data);
    }

    /**查询积分变更
     * @param Request $request
     * @return \think\response\Json
     */
    public function integralList(Request $request){
        $userid = $request->param('userid',null);
        $department_id = $request->param('department_id',null);
        $company_id = $request->param('company_id',null);
        if($userid){
            $data = Page::Integral(['integral_change.userid'=>$userid]);
        }elseif ($department_id){
            $data = Page::Integral(['integral_change.department_id'=>$department_id]);
        }elseif ($company_id){
            $data = Page::Integral(['integral_change.company_id'=>$company_id]);
        }else{
            $data = Page::Integral();
        }
        return json($data);
    }

    /**查询职位发布
     * @param Request $request
     * @return mixed
     */
    public function releaseList(Request $request){
       $position_id = $request->param('position_id',null);
       $department_id = $request->param('department_id',null);
       $company_id = $request->param('company_id',null);
       if($position_id){
           $data = Page::Release(['post_release.position_id'=>$position_id]);
       }elseif ($department_id){
           $data = Page::Release(['post_release.department_id'=>$department_id]);
       }elseif ($company_id){
           $data = Page::Release(['post_release.company_id'=>$company_id]);
       }else{
           $data = Page::Release();
       }
       return json($data);
    }

    /**查询评论
     * @param Request $request
     * @return \think\response\Json
     */
    public function commentList(Request $request){
        $userid = $request->param('userid',null);
        $department_id = $request->param('department_id',null);
        $company_id = $request->param('company_id',null);
        if($userid){
            $data = Page::Comment(['comment.userid'=>$userid]);
        }elseif ($department_id){
            $data = Page::Comment(['comment.department_id'=>$department_id]);
        }elseif ($company_id){
            $data = Page::Comment(['comment.company_id'=>$company_id]);
        }else{
            $data = Page::Comment();
        }
        return json($data);
    }

    /**查询公司产品
     * @param Request $request
     * @return \think\response\Json
     */
    public function productsList(Request $request){
        $id = $request->param('id',null);
        if($id){
            $data = Page::Products(['id'=>$id]);
        }else{
            $data = Page::Products();
        }
        return json($data);
    }

    /**查询公司的各种券
     * @param Request $request
     * @return array
     */
    public function voucherList(Request $request){
        $voucher_type = $request->param('voucher_type',null);
        $limit = $request->param('limit',10);
        if($voucher_type){
            $data = VoucherModel::getVoucher()->where('voucher_type',$voucher_type)->paginate($limit)->toArray();
        }else{
            $data = VoucherModel::getVoucher()->paginate($limit)->toArray();
        }
        return $data;
    }

}